package com.software.levelupserver.utils;

import com.software.levelupserver.entity.Badge;
import com.software.levelupserver.entity.Grade;
import com.software.levelupserver.entity.User;
import com.software.levelupserver.service.BadgeService;
import com.software.levelupserver.service.GradeService;
import com.software.levelupserver.service.impl.BadgeServiceImpl;
import com.software.levelupserver.service.impl.GradeServiceImpl;

import javax.annotation.Resource;
import java.util.List;

public class GradeUtil {

//    @Resource
//    private static GradeService gradeService;
//    @Resource
//    private static BadgeService badgeService;

    public static int upGrade(User user, List<Badge> badgeList, List<Grade> gradeList){
        int gradeId = user.getGradeId();
        Double experience = user.getExperience();

//        //获取用户持有的所有徽章
//        List<Badge> badgeList = badgeService.getAllBadgeByUserId();
//
//        //获取等级经验值表
//        List<Grade> gradeList = gradeService.getAllGrade();

        //目标等级
        Grade grade = gradeList.get(gradeId);
        //升级所需经验值
        double upExperience = grade.getExperience();

        //是否满足升级条件
        if (experience >= upExperience){
            if (badgeList != null && badgeList.size() != 0){
                //用户持有徽章中的最高等级的徽章等级
                int badgeGrade1 = badgeList.get(0).getBadgeGrade();
                switch (grade.getGradeId()){
                    case 1:
                        if (badgeGrade1 >= 1){
                            return grade.getGradeId();
                        }
                        return -1;
                    case 2:
                        if (badgeList.size() >= 2){
                            if (badgeGrade1 >= 2){
                                //用户获得的等级第二高的徽章等级
                                int badgeGrade2 = badgeList.get(1).getBadgeGrade();
                                if (badgeGrade2 >= 1){
                                    return grade.getGradeId();
                                }
                            }
                        }
                        return -1;
                    case 3:
                        if (badgeList.size() >= 3){
                            if (badgeGrade1 >= 3){
                                //用户获得的等级第二高的徽章等级
                                int badgeGrade2 = badgeList.get(1).getBadgeGrade();
                                if (badgeGrade2 >= 2){
                                    //用户获得的等级第三高的徽章等级
                                    int badgeGrade3 = badgeList.get(2).getBadgeGrade();
                                    if (badgeGrade3 >= 1){
                                        return grade.getGradeId();
                                    }
                                }
                            }
                        }
                        return -1;
                    case 4:
                        if (badgeList.size() >= 3){
                            if (badgeGrade1 >= 4){
                                //用户获得的等级第二高的徽章等级
                                int badgeGrade2 = badgeList.get(1).getBadgeGrade();
                                if (badgeGrade2 >= 3){
                                    //用户获得的等级第三高的徽章等级
                                    int badgeGrade3 = badgeList.get(2).getBadgeGrade();
                                    if (badgeGrade3 >= 1){
                                        return grade.getGradeId();
                                    }
                                }
                            }
                        }
                        return -1;
                    case 5:
                        if (badgeList.size() >= 3){
                            if (badgeGrade1 >= 5){
                                //用户获得的等级第二高的徽章等级
                                int badgeGrade2 = badgeList.get(1).getBadgeGrade();
                                if (badgeGrade2 >= 4){
                                    //用户获得的等级第三高的徽章等级
                                    int badgeGrade3 = badgeList.get(2).getBadgeGrade();
                                    if (badgeGrade3 >= 2){
                                        return grade.getGradeId();
                                    }
                                }
                            }
                        }
                        return -1;
                    case 6:
                        if (badgeList.size() >= 4){
                            if (badgeGrade1 >= 6){
                                //用户获得的等级第二高的徽章等级
                                int badgeGrade2 = badgeList.get(1).getBadgeGrade();
                                if (badgeGrade2 >= 5){
                                    //用户获得的等级第三高的徽章等级
                                    int badgeGrade3 = badgeList.get(2).getBadgeGrade();
                                    if (badgeGrade3 >= 3){
                                        //用户获得的等级第四高的徽章等级
                                        int badgeGrade4 = badgeList.get(3).getBadgeGrade();
                                        if (badgeGrade4 >= 2){
                                            return grade.getGradeId();
                                        }
                                    }
                                }
                            }
                        }
                        return -1;
                    case 7:
                        if (badgeList.size() >= 4){
                            if (badgeGrade1 >= 7){
                                //用户获得的等级第二高的徽章等级
                                int badgeGrade2 = badgeList.get(1).getBadgeGrade();
                                if (badgeGrade2 >= 6){
                                    //用户获得的等级第三高的徽章等级
                                    int badgeGrade3 = badgeList.get(2).getBadgeGrade();
                                    if (badgeGrade3 >= 5){
                                        //用户获得的等级第四高的徽章等级
                                        int badgeGrade4 = badgeList.get(3).getBadgeGrade();
                                        if (badgeGrade4 >= 3){
                                            return grade.getGradeId();
                                        }
                                    }
                                }
                            }
                        }
                        return -1;
                    default:
                        return -1;
                }
            }
        }

        return -1;
    }

}
